<?php
class Mageex_Robot_Helper_Ebay extends Mage_Core_Helper_Abstract
{
	public function ebayPost($keyword, $num, $start, $ebaycat)
	{
		if($keyword == "") {
			$return["error"]["reason"] = "No keyword";
			$return["error"]["message"] = Mage::helper('robot')->__("No keyword specified.");
			return $return;	
		}
		
		$country 	= Mage::helper('robot/config')->ebayCountry();
		$sortby 	= Mage::helper('robot/config')->ebaySortBy();
		$lang 		= Mage::helper('robot/config')->ebayLanguage();
		$affkey 	= Mage::helper('robot/config')->ebayAffKey();
		if (empty($ebaycat) || $ebaycat == "all"){$ebaycat="-1";}	
		
		$keyword = str_replace('"',"",$keyword );	
		$keyword = str_replace(" ","+",$keyword);
		$keyword = str_replace("-","+",$keyword);
		$keyword = strtolower($keyword);
		
		$rssurl = "http://rss.api.ebay.com/ws/rssapi?FeedName=SearchResults&siteId=$country&language=$lang&output=RSS20&sacat=$ebaycat&fcl=3&satitle=" . $keyword."&sacur=0&frpp=100&afepn=" . urlencode($affkey) . "&dfsp=32&sabfmts=0&salic=$country&ftrt=1&ftrv=1&customid=" .$keyword."&fss=0&saobfmts=exsif&catref=C5&saaff=afepn&from=R6&saslop=1";
		
		if($sortby !="bestmatch"){
			$rssurl.=$sortby;
		}
		
		echo $rssurl;
		if ( function_exists('curl_init') ) {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Konqueror/4.0; Microsoft Windows) KHTML/4.0.80 (like Gecko)");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_URL, $rssurl);
			curl_setopt($ch, CURLOPT_TIMEOUT, 60);
			$response = curl_exec($ch);	
			if (!$response) {
				$return["error"]["reason"] = "cURL Error";
				$return["error"]["message"] = Mage::helper('robot')->__("cURL Error Number ").curl_errno($ch).": ".curl_error($ch);	
				return $return;
			}
			curl_close($ch);
		} else { 				
			$response = @file_get_contents($rssurl);
			if (!$response) {
				$return["error"]["reason"] = "cURL Error";
				$return["error"]["message"] = Mage::helper('robot')->__("cURL is not installed on this server!");	
				return $return;		
			}
		}
		$therss = simplexml_load_string($response);
		
		$i = 0; $x = 0;
		$auctions = array();
		if ($therss){		
			if($therss->channel->item == "" || $therss->channel->item == null) {
				
				$auctions["error"]["reason"] = "No content";
				$auctions["error"]["message"] = Mage::helper('robot')->__("No auctions found for this keyword.");	
				return $auctions;	
			} else {
				//echo count($therss->channel->item);
				if($start > count($therss->channel->item)) {$start = 0;}
				$totalresults = $start + $num;
				foreach ($therss->channel->item as $item) { 
					if($i >= $start) {
						
						$thelink = $item->link;
						$theurl = $thelink;

						$descr = preg_replace ('#\$#', '&#36;',$item->description);
						
						if (count($auctions) < $num) {
							preg_match_all('#<td>(.*)<\/td>#iU', $item->description, $matches);
							$thumbnail = $matches[0][0];	
							$description = $matches[0][1];
							
							preg_match('#<strong>(.*)<\/strong>#iU', $description, $pricem);	
							$price = $pricem[1];	
							$ebcontent = "<strong>{title}</strong>{descriptiontable}";
							$ebcontent = str_replace("{thumbnail}", $thumbnail, $ebcontent);
							$ebcontent = str_replace("{price}", $price, $ebcontent);			
							$ebcontent = str_replace('{title}', $item->title, $ebcontent);
							$ebcontent = str_replace("{descriptiontable}", $item->description, $ebcontent);			
							$ebcontent = str_replace("{description}", $description, $ebcontent);
							$ebcontent = str_replace("{url}", $item->link, $ebcontent);
							if ($options['wpr_openlinks']=='yes') {
								$ebcontent = str_replace("<a ", '<a target="_blank" ', $ebcontent);
							}		
							if($item->title != "") {
								
							} else {
								$ebcontent = "";
							}
							
							$auctions[$x]["title"] = preg_replace ('#\$#', '&#36;',$item->title);	
							$auctions[$x]["price"] = $price;
							$auctions[$x]["descriptiontable"] = $item->description;
							$auctions[$x]["content"] = $ebcontent;
							$auctions[$x]["guid"] = $item->guid;
							$auctions[$x]["pubDaate"] = $item->pubDate;
							$x++;
						} else {
							break;
						}
					}
					$i++;
				}			
			}		
		} else {
			$auctions["error"]["reason"] = "Request fail";
			$auctions["error"]["message"] = Mage::helper('robot')->__("Ebay RSS feed could not be retreived.");	
			return $auctions;			
		}
		return $auctions;
	}
	
	public function rssHandler($attributes, $content = null)
	{
	
	}
}